<template>
  <div class="lb-agentAuth">
    <lb-title></lb-title>
    <div class="page-main search-form">
      <el-row class="page-top-operate">
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="$router.push('/editAgentAuth')"
          >{{ $t('menu.AddAgentAuth') }}</el-button
        >
      </el-row>
      <div class="page-top-operate form-search">
        <el-form @submit.native.prevent :inline="true" :model="searchForm">
          <el-form-item label="代理商状态">
            <el-select
              @change="getTableDataList(1)"
              v-model="searchForm.status"
              placeholder="请选择"
            >
              <el-option
                v-for="item in statusOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="代理等级">
            <el-select
              @change="getTableDataList(1)"
              v-model="searchForm.level"
              filterable
              clearable
              placeholder="请选择"
            >
              <el-option
                v-for="item in agentLevelSelect"
                :key="item.id"
                :label="item.title"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="代理地区">
            <el-cascader
              size="large"
              :options="areaOptions"
              v-model="selectedOptions"
              @change="handleChange"
              placeholder="请选择代理地区"
              :props="{ checkStrictly: true }"
              clearable
            ></el-cascader>
          </el-form-item>
          <el-form-item label="姓名">
            <el-input
              v-model="searchForm.user_name"
              placeholder="请输入代理商姓名"
            ></el-input>
          </el-form-item>
          <lb-button type="primary" @click="getTableDataList(1)">{{
            $t('action.search')
          }}</lb-button>
        </el-form>
      </div>
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        stripe
        style="width: 100%"
      >
        <el-table-column prop="id" label="ID" width="80px"></el-table-column>
        <el-table-column prop="user_name" label="姓名"></el-table-column>
        <el-table-column prop="mobile" label="电话"></el-table-column>
        <el-table-column prop="level_title" label="代理等级"></el-table-column>
        <el-table-column prop="province" label="地区">
          <template slot-scope="scope">
            <div class="table-operate">
              {{ scope.row.province + scope.row.city }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="开通时间">
          <template slot-scope="scope">
            <p>{{ scope.row.create_time | handleTime(1) }}</p>
            <p>{{ scope.row.create_time | handleTime(2) }}</p>
          </template>
        </el-table-column>
        <el-table-column prop="over_time" label="到期时间">
          <template slot-scope="scope">
            <p>{{ scope.row.over_time | handleTime(1) }}</p>
            <p>{{ scope.row.over_time | handleTime(2) }}</p>
          </template>
        </el-table-column>
        <el-table-column prop="have_count" label="开通数量"></el-table-column>
        <el-table-column prop="status_text" label="状态"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div class="table-operate">
              <el-button
                type="primary"
                size="mini"
                plain
                @click="editAuthInfo('edit', scope.row.id)"
                >{{ $t('action.edit') }}</el-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :page="searchForm.page"
        :pageSize="searchForm.page_count"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      ></lb-page>
    </div>
  </div>
</template>

<script>
import moment from 'moment'
import { provinceAndCityData } from 'element-china-area-data'
export default {
  name: 'auth',
  data () {
    return {
      loading: false,
      statusOptions: [
        {
          id: 0,
          name: '全部'
        },
        {
          id: 1,
          name: '使用中'
        },
        {
          id: 2,
          name: '已过期'
        }
      ],
      agentLevelSelect: [],
      areaOptions: provinceAndCityData,
      selectedOptions: [],
      searchForm: {
        page: 1,
        page_count: 10,
        user_name: '',
        status: 0,
        level: 0,
        province_code: '',
        city_code: ''
      },
      total: 0,
      tableData: []
    }
  },
  activated () {
    this.getTableDataList()
    this.getAgentLevelSelect()
  },
  methods: {
    getAgentLevelSelect () {
      this.$api.agentLevelSelect().then(res => {
        if (res.code === 200) {
          this.agentLevelSelect = res.data
          this.agentLevelSelect.unshift({
            id: 0,
            title: '全部'
          })
        }
      })
    },
    getTableDataList (flag) {
      this.loading = true
      if (flag) this.searchForm.page = 1
      let { searchForm } = this
      this.$api.agentAuthList(searchForm).then(res => {
        this.loading = false
        if (res.code === 200) {
          this.tableData = res.data.data
          this.total = res.data.total
        }
      })
    },
    editAuthInfo (type, id) {
      this.$router.push('/editAgentAuth?id=' + id)
    },
    handleSizeChange (val) {
      this.searchForm.page_count = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    },
    handleChange (value) {
      this.searchForm.province_code = value[0] || ''
      this.searchForm.city_code = value[1] || ''
      this.getTableDataList(1)
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-agentAuth {
  width: 100%;
}

.page-main .el-form .el-input {
  width: 200px;
}
</style>
